﻿package com.projects.xenia;

import java.util.ArrayList;

import com.projects.xenia.global_modules.MainGlobalModule;
import com.projects.xenia.ref_classes.GoodsRefItem;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemLongClickListener;

public class Activity_GoodsTree extends ListActivity {

	private EfficientAdapter adap;
	private ArrayList<GoodsRefItem> GoodsTree;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);            
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.goodstree_form);
		
		GoodsTree = MainGlobalModule.GoodsTreeRef;

		Bundle DocExtra = getIntent().getExtras();		
		String CurrentFolder = DocExtra.getString("CurrentFolder");
		
		adap = new EfficientAdapter(this);
		setListAdapter(adap);		
		
		FindPositionById (CurrentFolder);
		
		getListView().setOnItemClickListener(listener1);
		getListView().setOnItemLongClickListener(listener2);
		
	}	

	private void FindPositionById (String Id) {
		
		for (int i =0;i<GoodsTree.size();i++) {
			if (GoodsTree.get(i).id.equals(Id) ){
				getListView().setSelection(i);
				break;
			}
		}		
	}
	
	private void ReturnResult (int position, boolean Hierarchy) {
		
		GoodsRefItem gi = adap.getItem(position);			
		Intent DataIntent = new Intent ();			
		DataIntent.putExtra("GoodsTreeId",gi.id);
		DataIntent.putExtra("Hierarchy",Hierarchy);			
		setResult(RESULT_OK, DataIntent);
		finish();
		
	}
		
	private OnItemClickListener listener1 = new OnItemClickListener (){
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
			ReturnResult (position,true); 
		}	
	};
	
	private OnItemLongClickListener listener2 = new OnItemLongClickListener() {
		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int position, long arg3) {
			ReturnResult (position,false);			
			return true;	
		}
	
	};
		
	public class EfficientAdapter extends BaseAdapter  {
		
		private LayoutInflater mInflater;
		public Context context;
		
		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			this.context = context;
		}		
		 
		public View getView(final int position, View convertView, ViewGroup parent) {	
			
			ViewHolder holder;
		
			if (convertView == null) {			
				convertView = mInflater.inflate(R.layout.goodstree_form_list, null);
			}
			
			holder = new ViewHolder();
		
			holder.textLine = (TextView) convertView.findViewById(R.id.RestSumLabel);
			holder.imageLine = (ImageView) convertView.findViewById(R.id.imageViewLeft);		
				
			GoodsRefItem gi = getItem(position);			
			
			convertView.setPadding(gi.isfolder * 12, 0, 0,0);
			
			holder.textLine.setText(gi.descr);			
			holder.imageLine.setImageResource(R.drawable.ic_goodsfolder);			
			
			return convertView;
		
		}
		 
		class ViewHolder {
			
			ImageView imageLine;
			TextView textLine;
			
		}
		 
				 
		@Override
		public long getItemId(int position) {
			return 0;
		}
		 
		@Override
		public int getCount() {
			return GoodsTree.size();
		}
		 
		@Override
		public GoodsRefItem getItem(int position) {			
			
			return GoodsTree.get(position);
		}
	
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {		
		
		switch (keyCode){
		
    		case KeyEvent.KEYCODE_BACK:    				
    			setResult(RESULT_CANCELED);
    			finish();	
    		
    	}	
		
		return super.onKeyDown(keyCode, event);
	}
	
	
}
